using System.Collections.Generic;

namespace TLang.Values.Primitives
{
    using Ast;

    public class Eq : PrimFun
    {

        public Eq() : base("=", 2)
        {
            
        }


        public override Value Apply(List<Value> args, Node location)
        {

            Value v1 = args[0];
            Value v2 = args[1];
            if (v1 is IntValue v11 && v2 is IntValue v21)
            {
                return new BoolValue(v11.Value == v21.Value);
            }
            if (v1 is FloatValue lv && v2 is FloatValue rv)
            {
                return new BoolValue(lv.Value == rv.Value);
            }
            if (v1 is FloatValue fv1 && v2 is IntValue fv2)
            {
                return new BoolValue(fv1.Value == fv2.Value);
            }
            if (v1 is IntValue iv1 && v2 is FloatValue iv2)
            {
                return new BoolValue(iv1.Value == iv2.Value);
            }
            if (v1 is BoolValue bv1 && v2 is BoolValue bv2)
            {
                return new BoolValue(bv1.Value == bv2.Value);
            }
            if (v1 is StringValue sv1 && v2 is StringValue sv2)
            {
                return new BoolValue(sv1.Value == sv2.Value);
            }

            Util.Abort(location, "incorrect argument types for =: " + v1 + ", " + v2);
            return null;
        }


        public override Value Typecheck(List<Value> args, Node location)
        {
            Value v1 = args[0];
            Value v2 = args[1];

            if (!(v1 is IntType || v1 is FloatValue || v1 is BoolValue || v1 is StringValue) 
                || !(v2 is IntType || v2 is FloatValue || v2 is BoolValue || v2 is StringValue))
            {
                Util.Abort(location, "incorrect argument types for =: " + v1 + ", " + v2);
            }

            return Type.BOOL;
        }
    }
}